<template>
    <div class="coupon-detail">
        <div class="pic-img" ref="picImg">
            <img :src="couponDetail.couponpic">
        </div>
        <div class="detail-content-wrapper">
            <div class="info-wrapper">
                <van-row>
                    <van-col span="24">
                        <div class="coupon-name">{{couponDetail.couponName}}</div>
                    </van-col>
                </van-row>
                <van-row>
                    <van-col span="12">
                        <div class="receive-num">已有{{couponDetail.receiveNum}}人领取</div>
                    </van-col>
                    <van-col span="12">
                        <div class="exchange-btn">
                            <van-button round type="primary" @click="showPopUp">立即抢购</van-button>
                        </div>
                    </van-col>
                </van-row>
            </div>
            <div class="rule-wrapper">
                <van-row>
                    <van-col span="24">
                        <div class="date-text">有效期：</div>
                    </van-col>
                </van-row>
                <van-row>
                    <van-col span="24" >

                        <!-- 有效期类型 1-时间区间,2-小时内有效,3-截止日期,4-当日有效 -->
                        <div class="date-range">
                            <!-- <span v-if="couponDetail.expireDateType==2">
                                    领取后{{couponDetail.hours}}小时内有效
                            </span>
                            <span v-if="couponDetail.expireDateType==1">
                                    {{couponDetail.receiveStartTime}} - {{couponDetail.receiveEndTime}}
                            </span> -->
                            {{dateRange}}
                        </div>
                    </van-col>
                </van-row>
                <van-row>
                    <van-col span="24">
                        <div class="rules-text">使用规则：</div>
                    </van-col>
                </van-row>
                <van-row>
                    <van-col span="24" >
                        <div class="rules-info">
                            <section v-html="couponDetail.useRules"></section>
                        </div>
                    </van-col>
                </van-row>
            </div>
        </div>
        <div class="popup-wrapper">

            <van-popup v-model="popupShow" position="bottom" :lock-scroll="true">
                <div class="counpon-cart-wrapper">
                    <div class="title-wrapper">
                        <div class="title">
                            抢优惠券
                        </div>
                        <div class="cross-btn" @click="showPopUp">
                            <van-icon name="cross"/>
                        </div>
                    </div>

                    <div class="info-wrapper">
                        <van-row>
                            <van-col span="6">
                                <div class="logo">
                                    <img :src="couponDetail.logo">
                                </div>
                            </van-col>
                            <van-col span="18">
                                <div class="name">
                                    <span> {{couponDetail.couponName}}</span>
                                </div>
                            </van-col>
                        </van-row>
                    </div>
                    <div class="buy-num-wrapper">
                        <van-row>
                            <van-col span="18">
                                <div class="text">
                                    购买数量
                                </div>
                            </van-col>
                            <van-col span="6">
                                <div class="count">
                                    <span> 1</span>
                                </div>
                            </van-col>
                        </van-row>
                    </div>
                    <div class="buy-btn-wrapper">
                        <van-button round type="primary" @click="handlepickupCouponCreate">立即购买</van-button>
                    </div>
                </div>
            </van-popup>
        </div>
    </div>
</template>

<script>
    import Cookies from 'js-cookie'
    import {isDev,getScaleHeight} from "../../utils/tools";
    import {Toast} from 'vant';

    export default {
        inject:['reload'],
        name: "CouponDetail",
        data() {
            return {
                couponDetail: {},
                activityCpId: '',
                popupShow: false,
                perReceiveCondition:{
                    perdayLimit:0,
                    peroneLimit:0, //每人
                    perLimit:0  //每人每日
                },
                lockBtn:false
            }
        },
        created() {
        },
        mounted() {
            getScaleHeight(".pic-img")
            this.token = Cookies.get("token")
            this.activityCpId = this.$route.query.id
            isDev && console.log(this.token);
            this.fetchCouponDetail()
        },
        computed: {
            exchangeStatus() {
                if (this.perReceiveCondition.perdayLimit >= this.couponDetail.perdayLimit) return false //每日 (最大限制)
                if (this.perReceiveCondition.peroneLimit >= this.couponDetail.peroneLimit) return false //每人
                if (this.perReceiveCondition.perLimit >= this.couponDetail.perLimit) return false //每人每日
                return true
            },
            dateRange(){
                let expireDateType = this.couponDetail.expireDateType
                let expireDateRange =''
                if(expireDateType==1){
                     expireDateRange = this.couponDetail.expireDateStart+"~"+this.couponDetail.expireDateEnd
                }else if(expireDateType==2){
                      expireDateRange='领取后'+this.couponDetail.hours+'小时内有效'
                }else if(expireDateType==3){
                    //  领取后{{couponData.cutOffHour}}小时内有效,使用截止日期{{couponData.cutOffDate}}
                    expireDateRange = "领取后"+this.couponDetail.hours+"小时内有效,使用截止日期"+this.couponDetail.expireDateEnd
                }else if(expireDateType==4){
                    expireDateRange = "当日有效"
                }else{
                    console.log("无定义")
                }
                return expireDateRange
            }

        },
        methods: {
            fetchCouponDetail() {
                this.$api.coupons.pickupCouponDetail({id: this.activityCpId, openId: this.token}).then(res => {
                    console.log(res.data)
                    if (res.code == 0) {
                        this.couponDetail = res.data.couponDetail
                        this.perReceiveCondition = res.data.perReceiveCondition
                    }
                })
            },
            showPopUp() {
                this.popupShow = !this.popupShow
            },
            handlepickupCouponCreate() {
                if (!this.exchangeStatus){
                    Toast('已超过最大抢购次数!');
                    return
                }
                if(this.lockBtn)return
                this.lockBtn=true
                this.$api.coupons.pickupCouponCreate({activityCpId: this.activityCpId, openId: this.token}).then(res => {
                    if (res.code == 0) {
                        Toast.success('抢购成功!');
                        setTimeout(()=>{
                            this.lockBtn=false
                            this.reload()
                        },2000)
                        console.log(res);
                    } else {
                        Toast.fail(res.msg);
                        console.log(res);
                        this.lockBtn=false
                    }
                    this.showPopUp()
                }).catch(err => {
                    console.log(err);
                    this.lockBtn=false
                    this.showPopUp()
                })
            }
        }
    }
</script>

<style lang="less">
    @import "../../assets/less/base";

    .coupon-detail {
        min-height: 100%;
        background-color: @main-background-color;
        padding: 10px;
        .pic-img {
            height: 200px;
            overflow-y: hidden;
            img {
                width: 100%;
            }
        }
        .detail-content-wrapper {
            .info-wrapper {
                .coupon-name {
                    margin: 10px 0;
                    color: #627e94;
                    font-size: 20px;
                    font-weight: 700;
                }
                .receive-num {
                    height: 44px;
                    line-height: 44px;
                    padding-left: 4px;
                    color: #627e94;
                    font-size: 14px;
                    font-style: italic;
                }
                .exchange-btn {
                    text-align: right;
                    padding-right: 20px;
                    button {
                        background-color: @main-theme-color;
                        border: 1px solid @main-theme-color;
                        padding: 0 30px;
                    }
                }
            }

            .rule-wrapper {
                margin-top: 20px;
                .date-text {
                    color: #627e94;
                    background: url("/images/weixin2/hbg1.png") no-repeat;
                    background-size: contain;
                    padding-left: 6px;
                }
                .date-range {
                    color: #627e94;
                    .fz()
                }
                .rules-text {
                    color: #627e94;
                    background: url("/images/weixin2/hbg1.png") no-repeat;
                    background-size: contain;
                    padding-left: 6px;
                }
                .rules-info {
                    color: #627e94;
                    p {
                        margin-top: 0;
                        margin-bottom: 0;
                    }
                }
                .van-row {
                    margin-bottom: 20px;
                }
            }
        }
        .counpon-cart-wrapper {
            height: 300px;
            background-color: #f1f5f8;
            color: #627e94;
            .title-wrapper {
                position: relative;
                background-color: #ffffff;
                height: 40px;
                .title {
                    height: 40px;
                    line-height: 40px;
                    font-size: 20px;
                    text-align: center;
                    color: #627e94;
                }
                .cross-btn {
                    position: absolute;
                    top: 0;
                    right: 20px;
                    height: 40px;
                    line-height: 40px;
                    font-size: 20px;
                    color: @main-theme-color;
                }
            }
            .info-wrapper {
                margin: 10px 0;
                .logo {
                    margin-left: 6px;
                    width: 90px;
                    height: 90px;
                    img {
                        width: 90px;
                        height: 90px;
                    }
                }
                .name {
                    padding-left: 20px;
                }
            }
            .buy-num-wrapper {
                background-color: #ffffff;
                padding: 20px 0;
                .text {
                    font-size: 16px;
                    padding-left: 15px;
                }
                .count {
                    text-align: center;
                }
            }
            .buy-btn-wrapper {
                margin-top: 10px;
                text-align: center;
                button {
                    background-color: @main-theme-color;
                    border: 1px solid @main-theme-color;
                    padding: 0 60px;
                }
            }
        }
    }

</style>
